package com.fds.demo.alipay.controller;

import com.fds.demo.alipay.service.AliPayServiceImpl;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author: fds
 * @date: 2019/11/1
 * @description: 支付宝
 */
@RestController
@RequestMapping(value = "/aliPay")
public class AliPayController {

    /**
     * 注入 日志.
     */
    private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(AliPayController.class);

    /**
     * 注入 支付宝支付service.
     */
    @Autowired
    private AliPayServiceImpl aliPayService;

    /**
     * 支付宝支付.
     * @param orderNo 订单编号
     */
    @GetMapping("/pcPay")
    public void aliPay(final HttpServletResponse response, final String orderNo) {
        try {
            aliPayService.pcPay(response, orderNo);
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
        }
    }

    /**
     * 异步通知地址.
     * @param request 请求头
     * @return
     */
    @PostMapping("/notifyUrl")
    public String notifyUrl(final HttpServletRequest request) {
        try {
            return aliPayService.notifyUrl(request);
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
            return "fail";
        }
    }
}
